    <?php
		
				class AuthenticationDao extends BaseDao {
					
					/**
					 *
					 * @param string $username        	
					 * @param string $password        	
					 * @return Users
					 */
					
					public function getCredentials($username, $password) {
						if ($password == "") return false;
    					$customLDAP = new CustomLDAP();
						//custom, code for to aunthenticate in LDAP Server
						if (strcmp ( $username, "admin" ) != 0) {
/* 							$query = Doctrine_Query::create ()->from ( 'SystemUser' )->where ( 'user_name = ?', $username )->andWhere ( 'deleted = 0' );
							if ($query->fetchOne ()) {
								if ($this->ldap_aunthenticate ( $username, $password )) {

									
									return $query->fetchOne ();
								} else {
									
									$query = Doctrine_Query::create ()->from ( 'SystemUser' )->where ( 'user_name = ?', $username )->andWhere ( 'user_password = ?', md5 ( $password ) )->andWhere ( 'deleted = 0' );
										
									return $query->fetchOne ();
									
									
								}
							} else {
								
								return $query->fetchOne ();
							} */
							                            

							if ($this->ldap_aunthenticate ( $username, $password )) {
                               
                                $syncSuccess = $customLDAP->syncLdapUser($username);
								$query = Doctrine_Query::create ()->from ( 'SystemUser' )->where ( 'user_name = ?', $username )->andWhere ( 'deleted = 0' );
								return $query->fetchOne();
							}
                            else return false;
	
						} else {
							
							// for admin, end of custom code
							
							$query = Doctrine_Query::create ()->from ( 'SystemUser' )->where ( 'user_name = ?', $username )->andWhere ( 'user_password = ?', md5 ( $password ) )->andWhere ( 'deleted = 0' );
							
							$q = $query->fetchOne ();
							if ($q){
							    $customLDAP->syncAllLdapUsers();
							}
                            return $q;
						
						}
					}
					// function for to aunthenticate in normal way
					public function normal_authenticate($username, $password) {
							$query = Doctrine_Query::create ()->from ( 'SystemUser' )->where ( 'user_name = ?', $username )->andWhere ( 'user_password = ?', md5 ( $password ) )->andWhere ( 'deleted = 0' );
							
							return $query->fetchOne ();
					}
					
					// function for to aunthenticate in LDAP Server
					public function ldap_aunthenticate($username, $password) {
						
						// Connect to LDAP
						$ldapconn = ldap_connect ( LDAPConf::$ldap_host, LDAPConf::$ldap_port );
						if ($ldapconn) {
							
							// Sets the protocol version of the LDAP Server
							$tested = @ldap_set_option ( $ldapconn, LDAP_OPT_PROTOCOL_VERSION, 3 );
							@ldap_set_option ( $ldapconn, LDAP_OPT_REFERRALS, 0 );
							
							if ($tested) {
							    $customLDAP = new CustomLDAP();
								$filter = $customLDAP->userOnGroupFilter($username, LDAPConf::LDAP_EMPLOYEE_GID_NUMBER);
								$search_result = @ldap_search ( $ldapconn, LDAPConf::$ldap_baseDN, $filter );
								
								// verify user and password
								
								if ($search_result) {
									
									$result = @ldap_get_entries ( $ldapconn, $search_result );
									if ($result [0]) {
										
										if (@ldap_bind ( $ldapconn, $result [0] ['dn'], $password )) {
											ldap_unbind ( $ldapconn );
											return true;
										}
									}
								}
							}
						}
						
						return false;
					}
				}

